package gavdi.dk.controller;

import gavdi.dk.data.DAOAbsence;
import gavdi.dk.model.AbsenceRegistration;
import gavdi.dk.model.Absence;
import gavdi.dk.model.TimeRegistration;
import gavdi.dk.util.Tools;
import gavdi.dk.view.R;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestListener;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;





public class AbsenceAdapter extends ArrayAdapter<Absence>{

    Context context; 
    int layoutResourceId;    
    static List<Absence> Absence;
    protected static List<Absence> testList;
    

    private static class AbsenceHolder {
    	int position;
        TextView txtTitle;
        TextView txtTitle2;
    }
    

    
    public AbsenceAdapter(Context context, int layoutResourceId, List<Absence> Absence) {
        super(context, layoutResourceId, Absence);
        System.out.println("test1");
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        AbsenceAdapter.Absence = Absence;


    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    	System.out.println("getView " + position + " " + convertView);

        AbsenceHolder holder;

        if(convertView == null)
        {
        	
		//	LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        	LayoutInflater inflater = LayoutInflater.from(context); 
        	convertView = inflater.inflate(layoutResourceId, null);

            holder = new AbsenceHolder();
            holder.txtTitle = (TextView)convertView.findViewById(R.id.txtTitle);
            holder.txtTitle2 = (TextView)convertView.findViewById(R.id.txtTitle2);

            
            holder.position = position;

            convertView.setTag(holder);
        }
        else {
        	holder = (AbsenceHolder) convertView.getTag();
        }
		      
        new AbsenceTask(position, holder)
        .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null);
     
        	return convertView;
        	
        	
    }
    
    private static class AbsenceTask extends AsyncTask {
        private int ePosition;
        private AbsenceHolder eHolder;
        

        public AbsenceTask(int position, AbsenceHolder holder) {
            ePosition = position;
            eHolder = holder;
        }

   

		@Override
		protected Object doInBackground(Object... params) {
		
	

			
			return null;
		}
		
		@Override
		protected void onPostExecute(Object result) {
		      // if (eHolder.position == ePosition) {
		    	
			
		
				Absence a = Absence.get(ePosition);
			
			
				eHolder.txtTitle.setText(a.getName());
				eHolder.txtTitle2.setText(a.getCode());

		    	
		       //}
		       
	//	 

		}





    }

    
    

  
    
}